package org.xs.web.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class Params {
	public static final String P_SPLIT ="^_:_^";
	public static final String ACT="act";
	@SuppressWarnings("unchecked")
	public static Map<String, String> extractRequestParams(HttpServletRequest request){
		Map<String, String> results = new HashMap<String, String>();
		Map<String,String[]>  params = request.getParameterMap();	
		for(Map.Entry<String, String[]> entry : params.entrySet()){
			results.put(entry.getKey(), StringUtils.join(entry.getValue(),P_SPLIT));
		}
		return results;
	}
	
	public static Map<String, String> toUTF8(Map<String, String> params){
		for(Entry<String, String> entry : params.entrySet()){
			try {
				if(entry.getValue() != null){
					continue;
				}
				params.put(entry.getKey(), new String(entry.getValue().getBytes("ISO8859-1"),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				 
			}
		 	
		}
		return params;
		 
	}
	
	 
	public static Map<String, String> extractRequestParamsIgnoreArray(HttpServletRequest request){
		return extractRequestParamsIgnoreArray(request, false);
	}
	
	public static Map<String, String> extractRequestParamsIgnoreArray(HttpServletRequest request,boolean IfUTF8Encode){
		Map<String, String> results = new HashMap<String, String>();
		Map<String, String[]> params = request.getParameterMap();
		for(Map.Entry<String, String[]> entry : params.entrySet()){
			if(entry.getKey().equals("act")){
				continue;
			} 
			if(StringUtils.isBlank(request.getParameter(entry.getKey()))){
				results.put(entry.getKey(), null);
				 continue; 
			 }
			 if(IfUTF8Encode){
				 try {
					results.put(entry.getKey(), new String(request.getParameter(entry.getKey()).getBytes("ISO8859-1"),"UTF-8"));
				 	} catch (UnsupportedEncodingException e) {
				 }
			 }else{
				 results.put(entry.getKey(), request.getParameter(entry.getKey()));
			 }
			
		}
		return results;
	}
	public static Map<String, Object> params(HttpServletRequest request){
		Map<String, Object> results = new HashMap<String, Object>();
		Map<String,String[]>  params = request.getParameterMap();	
		for(Map.Entry<String, String[]> entry : params.entrySet()){
			if(entry.getValue().length > 0){
				if(!ArrayUtils.isEmpty(entry.getValue())){
					if(entry.getValue().length == 1){
						results.put(entry.getKey(), entry.getValue()[0]);
					}else{
						results.put(entry.getKey(), entry.getValue());
					}
				}
			}
			 
		}
		String querStr = request.getQueryString();
		querStr = querStr == null ? "" :Helpers.decode(querStr);
		String[] pv = querStr.split("&");
		if(pv.length > 0){
			for(String kv : pv){
				if(kv.length() > 0){
					String[] k_v = kv.split("=");
					if(k_v.length > 1){
							results.put(k_v[0], k_v[1]);
					}else{
							results.put(k_v[0], "");
						 
					}
				}
			}
		}
		return results;
	}
	
	 
	
}